home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 02 Useful Techniques / 03 Orkin / ActionTable.cpp next >
Encoding:
C/C++ Source or Header  |  2001-08-20  |  5.5 KB  |  164 lines

  1. #include "ActionTable.h"
  2.  
  3. //
  4. // This is an optimized version of the ActionTable.
  5. // It uses one multimap rather than nested maps.
  6. //
  7.  
  8. // --------------------------------------------------------------------------
  9.  
  10. const char* CActionTable::GetAnimation(EnumAnimCondition eAnimCond, EnumAction eAction, EnumActionDescriptor* peActionDesc)
  11. {
  12.     unsigned long key = CREATE_KEY(eAnimCond, eAction);
  13.  
  14.     CONDITION_ACTION_MAP::iterator ca_it;
  15.     ca_it = m_condActionMap.find(key);
  16.  
  17.     // Get list of actions for this animation condition.
  18.     if(ca_it != m_condActionMap.end())
  19.     {
  20.         ActionAnimInfoStruct* pAnimInfoStruct = NULL;
  21.  
  22.         // Get number of animations listed for this action.
  23.         long nCount = m_condActionMap.count(key);
  24.     
  25.         // If only one action is listed, return the animation.
  26.         if(nCount == 1)
  27.         {
  28.             pAnimInfoStruct = &(ca_it->second);
  29.  
  30.             if(peActionDesc != NULL)
  31.             {
  32.                 *peActionDesc = pAnimInfoStruct->eActionDesc;
  33.             }
  34.             return pAnimInfoStruct->szAnimFileName;
  35.         }
  36.  
  37.         // Pick randomly from a list of animations for this action.
  38.         else if(nCount > 1)
  39.         {
  40.             long nIndex = (long) ( ( (float)rand() / (float)RAND_MAX ) * (float)nCount );
  41.             for(long i=0; i<nIndex; ++i, ++ca_it);
  42.             pAnimInfoStruct = &(ca_it->second);
  43.  
  44.             if(peActionDesc != NULL)
  45.             {
  46.                 *peActionDesc = pAnimInfoStruct->eActionDesc;
  47.             }
  48.             return pAnimInfoStruct->szAnimFileName;
  49.         }
  50.     }
  51.  
  52.     // No animation was found for the specified eAnimCond and eAction,
  53.     // so see if a default animation exists.
  54.     if(eAnimCond != kACond_Default)
  55.     {
  56.         return GetAnimation(kACond_Default, eAction, peActionDesc);
  57.     }
  58.  
  59.     return NULL;
  60. }
  61.  
  62. // --------------------------------------------------------------------------
  63.  
  64. void CActionTable::Read() 
  65. {
  66.     // Write code to read the action table data in from a file here.
  67.     // For simplification, I'm hardcoding some entries.
  68.  
  69.     //
  70.     // ---- BEGIN STUB CODE -------
  71.     //
  72.  
  73.     unsigned long key;
  74.     ActionAnimInfoStruct aaiStruct;
  75.  
  76.     // Create some default animations.
  77.  
  78.     aaiStruct.eActionDesc = kADesc_None;
  79.  
  80.     strcpy(aaiStruct.szAnimFileName, "default_idle_scratchhead.anm");
  81.     key = CREATE_KEY(kACond_Default, kAct_Idle);
  82.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  83.  
  84.     strcpy(aaiStruct.szAnimFileName, "default_idle_lookaround.anm");
  85.     key = CREATE_KEY(kACond_Default, kAct_Idle);
  86.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  87.  
  88.     strcpy(aaiStruct.szAnimFileName, "default_idle_flex.anm");
  89.     key = CREATE_KEY(kACond_Default, kAct_Idle);
  90.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  91.  
  92.     strcpy(aaiStruct.szAnimFileName, "default_walk.anm");
  93.     key = CREATE_KEY(kACond_Default, kAct_Walk);
  94.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  95.  
  96.     strcpy(aaiStruct.szAnimFileName, "default_run.anm");
  97.     key = CREATE_KEY(kACond_Default, kAct_Run);
  98.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  99.  
  100.     strcpy(aaiStruct.szAnimFileName, "default_attack.anm");
  101.     key = CREATE_KEY(kACond_Default, kAct_Attack);
  102.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  103.  
  104.  
  105.     // Create some one-handed weapon animations.
  106.  
  107.     aaiStruct.eActionDesc = kADesc_None;
  108.  
  109.     strcpy(aaiStruct.szAnimFileName, "one_handed_idle.anm");
  110.     key = CREATE_KEY(kACond_OneHanded, kAct_Idle);
  111.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  112.  
  113.     strcpy(aaiStruct.szAnimFileName, "one_handed_walk.anm");
  114.     key = CREATE_KEY(kACond_OneHanded, kAct_Walk);
  115.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  116.  
  117.     strcpy(aaiStruct.szAnimFileName, "one_handed_run.anm");
  118.     key = CREATE_KEY(kACond_OneHanded, kAct_Run);
  119.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  120.  
  121.     aaiStruct.eActionDesc = kADesc_Swing;
  122.     strcpy(aaiStruct.szAnimFileName, "one_handed_attack_swing.anm");
  123.     key = CREATE_KEY(kACond_OneHanded, kAct_Attack);
  124.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  125.  
  126.     aaiStruct.eActionDesc = kADesc_Jab;
  127.     strcpy(aaiStruct.szAnimFileName, "one_handed_attack_jab.anm");
  128.     key = CREATE_KEY(kACond_OneHanded, kAct_Attack);
  129.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  130.  
  131.  
  132.     // Create some two-handed weapon animations.
  133.  
  134.     aaiStruct.eActionDesc = kADesc_None;
  135.  
  136.     strcpy(aaiStruct.szAnimFileName, "two_handed_idle.anm");
  137.     key = CREATE_KEY(kACond_TwoHanded, kAct_Idle);
  138.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  139.  
  140.     strcpy(aaiStruct.szAnimFileName, "two_handed_walk.anm");
  141.     key = CREATE_KEY(kACond_TwoHanded, kAct_Walk);
  142.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  143.  
  144.     strcpy(aaiStruct.szAnimFileName, "two_handed_run.anm");
  145.     key = CREATE_KEY(kACond_TwoHanded, kAct_Run);
  146.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  147.  
  148.     aaiStruct.eActionDesc = kADesc_Swing;
  149.     strcpy(aaiStruct.szAnimFileName, "two_handed_attack_swing.anm");
  150.     key = CREATE_KEY(kACond_TwoHanded, kAct_Attack);
  151.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  152.  
  153.     aaiStruct.eActionDesc = kADesc_Jab;
  154.     strcpy(aaiStruct.szAnimFileName, "two_handed_attack_jab.anm");
  155.     key = CREATE_KEY(kACond_TwoHanded, kAct_Attack);
  156.     m_condActionMap.insert( CONDITION_ACTION_MAP::value_type( key, aaiStruct) );
  157.  
  158.     //
  159.     // ---- END STUB CODE -------
  160.     //
  161. }
  162.  
  163. // --------------------------------------------------------------------------
  164.